package com.isay.app.honey.ui.mine;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.isay.app.honey.R;
import com.isay.app.honey.manager.NetworkManager;
import com.isay.app.honey.ui.cash.CashManagerActivity;
import com.isay.app.honey.ui.invite.InviteActivity;
import com.isay.app.honey.ui.login.LoginActivity;
import com.isay.app.honey.ui.modify.ModifyPwdActivity;
import com.isay.app.honey.ui.profit.MyProfitActivity;
import com.isay.frameworklib.okhttp.ICommonCallback;
import com.isay.frameworklib.ui.BaseFragment;
import com.isay.frameworklib.user.UserManager;
import com.isay.frameworklib.utils.ToastUtils;

import org.json.JSONObject;

/**
 * Created by cywu4 on 2018/8/9.
 */

public class MineFragment extends BaseFragment implements View.OnClickListener {


    private TextView mTvWeixin;
    private TextView mTvUserName;
    private TextView mTvInviteCode;
    private TextView mTvInviteNum;

    public static MineFragment getInstance() {
        return new MineFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }


    @Override
    protected void initView(View rootView) {
        rootView.findViewById(R.id.lay_invite).setOnClickListener(this);
        rootView.findViewById(R.id.lay_modify_pwd).setOnClickListener(this);
        rootView.findViewById(R.id.btn_login_out).setOnClickListener(this);
        mTvWeixin = rootView.findViewById(R.id.tv_weixin);
        mTvUserName = rootView.findViewById(R.id.tv_user_name);
        mTvInviteCode = rootView.findViewById(R.id.tv_invite_code);
        mTvInviteNum = rootView.findViewById(R.id.tv_invite_num);

    }

    @Override
    protected void initData() {
        userCenter();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lay_invite:
                InviteActivity.launch(getActivity());
                break;
            case R.id.lay_modify_pwd:
                ModifyPwdActivity.launch(getActivity());
                break;
            case R.id.btn_login_out:
                UserManager.setUserPwd("");
                LoginActivity.launch(getActivity());
                getActivity().finish();
                break;
            default:
                break;
        }
    }


    //个人中心
    private void userCenter() {
        NetworkManager.userCenter(new ICommonCallback() {
            @Override
            public void onSuccess(int ret, String msg, String content) {
                if (ret == 200) {
                    try {
                        JSONObject jsonObject = new JSONObject(content);
                        JSONObject userObj = jsonObject.optJSONObject("user");
                        String username = userObj.optString("username");
                        String invitecode = userObj.optString("invitecode");
                        String num = userObj.optString("num");
                        mTvUserName.setText(username);
                        mTvInviteCode.setText("ID：" + invitecode);
                        mTvInviteNum.setText("邀请人数：" + num);
                        String wxCode = jsonObject.optString("wxcode");
                        mTvWeixin.setText("微信号：" + wxCode);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.show(msg);
                }
            }

            @Override
            public void onFailure(Exception e) {
                ToastUtils.show(e.toString());
            }
        });
    }

}
